package com.uphone.kingmall.activity.personal;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.HistoryListAdapter;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseListActivity;
import com.uphone.kingmall.bean.HistoryGoodsBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.HttpParamsUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PopupUtil;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/22
 *     desc   :
 * </pre>
 */
public class HistoryListActivity extends BaseListActivity<HistoryGoodsBean, HistoryGoodsBean.DataBean> {

    private HistoryListAdapter adapter;
    private PopupUtil popupFilter;
    private CheckBox iv_select;

    @Override
    protected String initTitle() {
        return "足迹";
    }

    @Override
    protected void setTitle(final DefaultNavigationBar navigationBar) {
        super.setTitle(navigationBar);
        navigationBar.setRightTitle("完成");
        navigationBar.getRightTitle().setVisibility(View.GONE);
        navigationBar.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigationBar.getRightIcon().setVisibility(View.VISIBLE);
                v.setVisibility(View.GONE);
                adapter.setManager(false);
                if (popupFilter != null) {
                    popupFilter.dismiss();
                }
            }
        });

        navigationBar.setRightIcon(R.mipmap.ic_delete);
        navigationBar.setIvRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 变成删除
                navigationBar.getRightTitle().setVisibility(View.VISIBLE);
                v.setVisibility(View.GONE);
                adapter.setManager(true);
                showPopup();
            }
        });
    }

    private void showPopup() {
        View inflate = View.inflate(this, R.layout.kingmall_popup_history_delete, null);
        popupFilter = PopupUtil.getInstance(MyApplication.mContext, inflate);
        popupFilter.setOutsideTouchable(false).setFocusable(false).showBottom();
        popupFilter.setDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (popupFilter != null) {
                    popupFilter.release();
                }
            }
        });

        iv_select = inflate.findViewById(R.id.ivSelectAll);
        TextView tv_delete = inflate.findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
                if (iv_select != null) {
                    iv_select.setChecked(false);
                }
            }
        });
        iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = iv_select.isChecked();
                for (int i = 0; i < dataList.size(); i++) {
                    dataList.get(i).setSelect(isChecked);
                    for (int j = 0; j < dataList.get(i).getDateFoots().size(); j++) {
                        dataList.get(i).getDateFoots().get(j).setSelect(isChecked);
                    }
                }
                adapter.setNewData(dataList);
            }
        });
    }

    private void delete() {
        String shopCarId = "";
        if (dataList != null && dataList != null && dataList.size() > 0) {
            for (int i = 0; i < dataList.size(); i++) {
                if (dataList.get(i).getDateFoots() != null && dataList.get(i).getDateFoots().size() > 0) {
                    for (int j = 0; j < dataList.get(i).getDateFoots().size(); j++) {
                        HistoryGoodsBean.DataBean.DateFootsBean goodsBean = dataList.get(i).getDateFoots().get(j);
                        if (goodsBean.isSelect()) {
                            shopCarId += goodsBean.getFootId() + ",";
                        }
                    }
                }
            }
        }
        if (TextUtils.isEmpty(shopCarId)) {
            ToastUtils.showShortToast(this, "您还没有选择商品");
            return;
        }
        String str = shopCarId.substring(0, shopCarId.length() - 1);
        HttpParams params = new HttpParams();
        params.put("footIds", str);

        OkGoUtils.progressRequest(MyUrl.delUserFoot, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    // 刷新
                    loadData(1);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupFilter != null) {
            popupFilter.release();
        }
    }

    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        adapter = new HistoryListAdapter(this);
        adapterListener(adapter);
        return adapter;
    }

    @Override
    protected HistoryGoodsBean getBeans() {
        return new HistoryGoodsBean();
    }

    @Override
    protected HttpParams getParams() {
        refresh();
        return HttpParamsUtils.getInstance().put("page", page).put("limit", 12).getParams();
    }

    @Override
    protected String getUrl() {
        return MyUrl.getUserGoodsFoots;
    }

    private void adapterListener(HistoryListAdapter adapter) {

        adapter.setOnShopItemClickListener(new HistoryListAdapter.OnShopItemClickListener() {
            @Override
            public void itemClick(View v, int position, HistoryGoodsBean.DataBean item) {

            }

            @Override
            public void itemSelect(View v, int position, HistoryGoodsBean.DataBean item) {
                // 日期选中
                itemSelectChange(position, item);
            }

            @Override
            public void itemGoodClick(View v, int childPosition, HistoryGoodsBean.DataBean.DateFootsBean childItem) {

            }

            @Override
            public void itemGoodSelect(View v, int position, int childPosition, HistoryGoodsBean.DataBean item, HistoryGoodsBean.DataBean.DateFootsBean childItem) {
                // 商品选中
                itemSelectChange(position, item);
            }
        });
    }

    private void itemSelectChange(int position, HistoryGoodsBean.DataBean item) {
        dataList.set(position, item);
        refresh();
    }

    private void refresh() {
        if (popupFilter == null) {
            return;
        }
        if (iv_select == null) {
            return;
        }
        boolean check = true;
        for (int i = 0; i < dataList.size(); i++) {
            if (!dataList.get(i).isSelect()) {
                check = false;
                break;
            }
        }
        iv_select.setChecked(check);
    }
}
